# -*- coding: utf-8 -*-
import sys
import conf
from utils import message_dialog
from uimanager import UIManager
from ConfigParser import ConfigParser

try:
    import pygtk
    pygtk.require("2.0")
    import gtk
    import gtk.glade    
except ImportError,cause:
    sys.stderr.write("ImportError: Module is not present %s" % cause)
    sys.exit(1)

class MessageWindow():
    def __init__(self):
        self.uimanager = UIManager("message_window")
        self.window = self.uimanager.widget("message_window")
        self.window.set_title("Mensagem")

        # add button ok and cancel
        self.post_button = self.window.add_button("Enviar",gtk.RESPONSE_OK)
        self.window.add_button("Cancelar",gtk.RESPONSE_CANCEL)

        # set senstive false, means cannot send 
        self.post_button.set_sensitive(False)
        
        # set text buffer, is our area to write the message
        self.textbuffer = gtk.TextBuffer()
        self.textview = self.uimanager.widget("textview")

        # is visual limitator to text, max is 140 chars
        self.maxbar = self.uimanager.widget("maxbar")
        self.maxbar.set_fraction(0.0)
        self.maxbar.set_text("0")
        self.cc = 0
        self.textstring = ""
        
        # connect a important signal to limit the text
        self.uimanager.signal_connect("on_key_release",self.calc_post_size)
        
    def run(self):
        while True:
            response = self.window.run()

            # if the response is ok
            if response == gtk.RESPONSE_OK:

                # validate the 140 chars limit
                if not self._post_validate():
                    
                    # error a dialog will mess you
                    self.error_dialog()
                    continue

                # ok can pass and destroy this
                self._get_text()
                self.window.destroy()
                return True
                break;

            # cancel sent nothing
            elif response == gtk.RESPONSE_CANCEL:
                self.window.destroy()
                return False
                break;

            # delete window, sent nothing
            elif response == gtk.RESPONSE_DELETE_EVENT:
                self.window.destroy()
                return False
                break;

    # get the the text written
    def _get_text(self):
        start_itr, end_itr = self.textview.get_buffer().get_bounds()
        self.textstring = self.textview.get_buffer().get_text(start_itr,end_itr)

    # get the outside the class    
    def get_text(self):
        return self.textstring

    # validate the char limits
    def _post_validate(self):
        if self.cc > 140:
            return False
        return True
    
    # calculating the bar size
    def calc_post_size(self,widget,data = None):
        self.cc = self.textview.get_buffer().get_char_count()

        if(self.cc > 140):
            self.maxbar.set_text("Limite excedido")
            self.post_button.set_sensitive(False)
        else:            
            self.maxbar.set_text(str(self.cc))            
            self.maxbar.set_fraction(self.cc/140.0)                        
            self.post_button.set_sensitive(True)
 
    # show the warning
    def error_dialog(self):
        message_dialog("Limite de texto para envio excedido")
